package cn.tedu.egweibo5.base.exception;

import cn.tedu.egweibo5.base.response.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Justin Parker
 * @author 2024/8/15 22:33
 * @version 1.0
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public JsonResult doHandle(IllegalArgumentException ex){

        String data = ex.getMessage();
        return JsonResult.fail(data);
    }

    @ExceptionHandler
    public JsonResult doHandleMethodArgumentNotValidException(MethodArgumentNotValidException ex){
        String message = ex.getFieldError().getDefaultMessage();
        return JsonResult.fail(message);
    }
}
